﻿using System;
using System.Net;
using System.Windows;
using SimpleJson;
using System.Collections.Generic;

namespace MetroEventbrite.External.Eventbrite
{
    public class EBEvent
    {
        public static EBEvent Load(string json)
        {
            var result = new EBEvent();
            JsonObject jsonObj;
            try
            {

                jsonObj = (JsonObject)SimpleJson.SimpleJson.DeserializeObject(json);
            }
            catch (Exception ex)
            {
                return result;
            }
            return Load(jsonObj);
        }

        public static EBEvent Load(JsonObject jsonObj)
        {
            var result = new EBEvent();
            if (jsonObj.ContainsKey("id"))
            {
                result.TicketId = jsonObj["id"].ToString();
            }
            if (jsonObj.ContainsKey("title"))
            {
                result.Title = jsonObj["title"].ToString();
            }
            if (jsonObj.ContainsKey("description"))
            {
                result.Description = jsonObj["description"].ToString();
            }
            if (jsonObj.ContainsKey("tags"))
            {
                result.Tags = jsonObj["tags"].ToString().Split(',');
            }
            if (jsonObj.ContainsKey("start_date"))
            {
                result.StartDateTimeString = jsonObj["start_date"].ToString();
            }
            if (jsonObj.ContainsKey("end_date"))
            {
                result.EndDateTimeString = jsonObj["end_date"].ToString();
            }
            if (jsonObj.ContainsKey("timezone"))
            {
                result.Timezone = jsonObj["timezone"].ToString();
            }
            if (jsonObj.ContainsKey("created"))
            {
                result.CreateDateString = jsonObj["created"].ToString();
            }
            if (jsonObj.ContainsKey("modified"))
            {
                result.ModifiedDateString = jsonObj["modified"].ToString();
            }
            if (jsonObj.ContainsKey("privacy"))
            {
                result.IsPublic = jsonObj["privacy"].ToString().Equals("Public", StringComparison.OrdinalIgnoreCase);
            }
            if (jsonObj.ContainsKey("password"))
            {
                result.PrivateEventPassword = jsonObj["password"].ToString();
            }
            if (jsonObj.ContainsKey("capacity"))
            {
                result.Capacity = Convert.ToInt32(jsonObj["capacity"].ToString());
            }
            if (jsonObj.ContainsKey("num_attendee_rows"))
            {
                result.CurrentNumberOfRegistrations = Convert.ToInt32(jsonObj["num_attendee_rows"].ToString());
            }

            if (jsonObj.ContainsKey("url"))
            {
                result.Url = jsonObj["url"].ToString();
            }
            if (jsonObj.ContainsKey("logo"))
            {
                result.Logo = jsonObj["logo"].ToString();
            }
            if (jsonObj.ContainsKey("status"))
            {
                result.StatusString = jsonObj["status"].ToString();
            }
            if (jsonObj.ContainsKey("background_color"))
            {
                result.BackgroundColorString = jsonObj["background_color"].ToString();
            }
            if (jsonObj.ContainsKey("text_color"))
            {
                result.TextColorString = jsonObj["text_color"].ToString();
            }
            if (jsonObj.ContainsKey("link_color"))
            {
                result.LinkColorString = jsonObj["link_color"].ToString();
            }
            if (jsonObj.ContainsKey("title_text_color"))
            {
                result.TitleTextColorString = jsonObj["title_text_color"].ToString();
            }
            if (jsonObj.ContainsKey("box_background_color"))
            {
                result.BoxBackgroundColorString = jsonObj["box_background_color"].ToString();
            }
            if (jsonObj.ContainsKey("box_text_color"))
            {
                result.BoxTextColorString = jsonObj["box_text_color"].ToString();
            }
            if (jsonObj.ContainsKey("box_border_color"))
            {
                result.BoxBorderColorString = jsonObj["box_border_color"].ToString();
            }
            if (jsonObj.ContainsKey("box_header_background_color"))
            {
                result.BoxHeaderBackgroundColorString = jsonObj["box_header_background_color"].ToString();
            }
            if (jsonObj.ContainsKey("box_header_text_color"))
            {
                result.BoxHeaderTextColorString = jsonObj["box_header_text_color"].ToString();
            }
            if (jsonObj.ContainsKey("venue") && jsonObj["venue"] != null)
            {
                result.Venu = EBVenue.Load(jsonObj["venue"].ToString());
            }
            
            if (jsonObj.ContainsKey("organizer") && jsonObj["organizer"] != null)
            {
                result.Organizer = EBOrganizer.Load(jsonObj["organizer"].ToString());
            }
            if (jsonObj.ContainsKey("tickets") && jsonObj["tickets"] != null)
            {
                result.TicketTypesString = EBOrganizer.Load(jsonObj["tickets"].ToString());

                result.TicketTypes = new List<EBTicket>();
                var tickets = (JsonArray)jsonObj["tickets"];
                foreach (var item in tickets)
                {
                    var data = (JsonObject)item;
                    result.TicketTypes.Add(EBTicket.Load(data));
                }


            }
            return result;

        }

        public string TicketId { get; set; }

        public string Title { get; set; }

        public string Description { get; set; }

        public string[] Tags { get; set; }


        public string StartDateTimeString { get; set; }

        public string EndDateTimeString { get; set; }

        public string Timezone { get; set; }

        public string CreateDateString { get; set; }

        public string ModifiedDateString { get; set; }

        public bool IsPublic { get; set; }

        public string PrivateEventPassword { get; set; }

        public int Capacity { get; set; }

        public int CurrentNumberOfRegistrations { get; set; }

        public string Url { get; set; }

        public string Logo { get; set; }

        public string StatusString { get; set; }

        public string BackgroundColorString { get; set; }

        public string TextColorString { get; set; }

        public string LinkColorString { get; set; }

        public string TitleTextColorString { get; set; }

        public string BoxBackgroundColorString { get; set; }

        public string BoxTextColorString { get; set; }

        public string BoxBorderColorString { get; set; }

        public string BoxHeaderBackgroundColorString { get; set; }

        public string BoxHeaderTextColorString { get; set; }

        public EBVenue Venu { get; set; }

        public EBOrganizer Organizer { get; set; }

        public EBOrganizer TicketTypesString { get; set; }

        public List<EBTicket> TicketTypes { get; set; }
    }

}
